package countdownProtocol;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class CountdownClient extends Thread
{
	private int loops;
	private int port; 
	private String host;
	
	public CountdownClient (int loops, int port, String host)
	{
		this.port = port;
		this.loops = loops;
		this.host = host;
	}

	@Override
	public void run()
	{
		try
		{
			Socket socket = new Socket(InetAddress.getByName(host), port);
			
			OutputStream out = socket.getOutputStream();
			InputStream in = socket.getInputStream();
			
			out.write(loops);
			out.flush();
					
			for(int i = loops; i > 0; i--)
			{
				int current = in.read();
				
				if(current != -1)
				{
					System.out.println("From Server: "+current);
				}
				else
				{
					i++;
				}
			}
			
			socket.close();
			
		} catch (UnknownHostException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}
